package remoteView;

import java.util.ArrayList;
import java.util.Set;
import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.cloudgarden.resource.SWTResourceManager;

import controller.CommandsExcuter;
import controller.CommandsExcuter.AddFolderExcutor;
import controller.CommandsExcuter.GetFoldersExcutor;






/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AddSharedFolderBox {

	private Display display;
	private Shell shell;
	private int index;
	private Button prev;
	private Button next;
	private Vector<Control> PAGES;
	private Composite wizardPanel;
	private StackLayout wizardLayout;
	private Vector<String> TITLES;
	
	
	private Text folderNameText;
	private Table existFolderTable;
	private Label folderNameLabel;
	private Button newFolderOption;
	private Button existFolderOption;
	private Label label;
	private Label emailLabel;
	private Button cancelButton;
	private List emailList;
	private Button addButton;
	private Text emailText;
	private Button selectAllButton;
	private ArrayList<TableItem> checkSet=new ArrayList<TableItem>();
	private ArrayList<String> selectedFolders= new ArrayList<String>();
	private Set<String> foldersList;
	private Label errLabel;
	/**
	 * constructor of the class
	 */
	public AddSharedFolderBox() {
		//getting names of all shared folders of this client
		GetFoldersExcutor getFolders=CommandsExcuter.getCommand().getGetFolders();
		getFolders.excute();
		foldersList=getFolders.getReturnVal();
		
		display = Display.getDefault();
		shell = new Shell(display);
		
		shell.setText("Add shared folder wizard");
		shell.addShellListener(new ShellListener() {

			@Override
			public void shellActivated(ShellEvent arg0) {}

			@Override
			public void shellClosed(ShellEvent arg0) {
				shell.setVisible(false);
				arg0.doit=false;
			}

			@Override
			public void shellDeactivated(ShellEvent arg0) {}

			@Override
			public void shellDeiconified(ShellEvent arg0) {}

			@Override
			public void shellIconified(ShellEvent arg0) {}
			
		});
		
		
		init();
		createGUI();
		fillPages();
		setSize(500, 400);
		shell.open();
		while(!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}
	public static void main(String[] args) {
		new AddSharedFolderBox();
		
	}
	/**
	 * fill all composites in the wizard
	 */
	private void fillPages() {
		//first composite definition
		Composite inst = new Composite(shell, SWT.NULL);
		FormLayout thisLayout = new FormLayout();
		inst.setLayout(thisLayout);
		inst.setSize(406, 200);
		inst.layout();
		
		{
			FormData existFolderTableLData = new FormData();
			existFolderTableLData.left =  new FormAttachment(0, 1000, 26);
			existFolderTableLData.top =  new FormAttachment(0, 1000, 45);
			existFolderTableLData.width = 258;
			existFolderTableLData.height = 62;
			existFolderTable = new Table(inst, SWT.CHECK | SWT.BORDER);
			existFolderTable.setLayoutData(existFolderTableLData);
			existFolderTable.setLinesVisible(true);
			//adding check boxes of exist directories to the table
			for (String nameOfFolder: foldersList) {
			      TableItem item = new TableItem(existFolderTable, SWT.NONE);
			      item.setText(nameOfFolder);
			      checkSet.add(item);
			      
			}
			//exist folder were selected
			existFolderTable.addListener(SWT.Selection, new Listener() {
			      public void handleEvent(Event event) {
			          
			          if(event.detail == SWT.CHECK)
			        	  next.setEnabled(true);
			     }
			});
			
		}
		{
			folderNameLabel = new Label(inst, SWT.NONE);
			FormData folderNameLabelLData = new FormData();
			folderNameLabelLData.left =  new FormAttachment(0, 1000, 21);
			folderNameLabelLData.top =  new FormAttachment(0, 1000, 156);
			folderNameLabelLData.width = 37;
			folderNameLabelLData.height = 18;
			folderNameLabel.setLayoutData(folderNameLabelLData);
			folderNameLabel.setText("Name:");
		}
		{
			selectAllButton = new Button(inst, SWT.PUSH | SWT.CENTER);
			FormData selectAllButtonLData = new FormData();
			selectAllButtonLData.left =  new FormAttachment(0, 1000, 319);
			selectAllButtonLData.top =  new FormAttachment(0, 1000, 59);
			selectAllButtonLData.width = 70;
			selectAllButtonLData.height = 27;
			selectAllButton.setLayoutData(selectAllButtonLData);
			selectAllButton.setText("select all");
			//the "select all" button was selected
			selectAllButton.addSelectionListener(new SelectionListener(){

				@Override
				public void widgetDefaultSelected(SelectionEvent arg0) {}

				@Override
				public void widgetSelected(SelectionEvent arg0) {
					
					for (int i = 0; i < checkSet.size(); i++) {
						checkSet.get(i).setChecked(true);
					}
					
				}
				
			});
		}
		{
			newFolderOption = new Button(inst, SWT.RADIO | SWT.LEFT);
			FormData newFolderOptionLData = new FormData();
			newFolderOptionLData.left =  new FormAttachment(0, 1000, 6);
			newFolderOptionLData.top =  new FormAttachment(0, 1000, 131);
			newFolderOptionLData.width = 160;
			newFolderOptionLData.height = 16;
			newFolderOption.setLayoutData(newFolderOptionLData);
			newFolderOption.setText("Create new shared folder");
			newFolderOption.addSelectionListener(new SelectionListener(){

				@Override
				public void widgetDefaultSelected(SelectionEvent arg0) {}

				@Override
				public void widgetSelected(SelectionEvent arg0) {
					folderNameText.setEnabled(true);
					existFolderTable.setEnabled(false);
					selectAllButton.setEnabled(false);
					
				}
				
			});
		}
		{
			existFolderOption = new Button(inst, SWT.RADIO | SWT.LEFT);
			FormData existFolderOptionLData = new FormData();
			existFolderOptionLData.left =  new FormAttachment(0, 1000, 12);
			existFolderOptionLData.top =  new FormAttachment(0, 1000, 23);
			existFolderOptionLData.width = 134;
			existFolderOptionLData.height = 16;
			existFolderOption.setLayoutData(existFolderOptionLData);
			existFolderOption.setText("Share existing folder");
			existFolderOption.setSelection(true);
			existFolderOption.addSelectionListener(new SelectionListener(){

				@Override
				public void widgetDefaultSelected(SelectionEvent arg0) {}

				@Override
				public void widgetSelected(SelectionEvent arg0) {
					folderNameText.setEnabled(false);
					existFolderTable.setEnabled(true);
					selectAllButton.setEnabled(true);
					
				}
				
			});
		}
		{
			FormData folderNameTextLData = new FormData();
			folderNameTextLData.left =  new FormAttachment(0, 1000, 104);
			folderNameTextLData.top =  new FormAttachment(0, 1000, 156);
			folderNameTextLData.width = 278;
			folderNameTextLData.height = 15;
			folderNameText = new Text(inst, SWT.NONE | SWT.BORDER);
			folderNameText.setLayoutData(folderNameTextLData);
			folderNameText.setEnabled(false);
			folderNameText.addKeyListener(new KeyListener() {
				
				@Override
				public void keyReleased(KeyEvent arg0) {
					next.setEnabled(true);
					if(folderNameText.getText().equals(""))
						next.setEnabled(false);
				}
				
				@Override
				public void keyPressed(KeyEvent arg0) {
					
						
					
				}
			});
		}
		{
			errLabel = new Label(inst, SWT.NONE);
			FormData errLabelLData = new FormData();
			errLabelLData.left =  new FormAttachment(0, 1000, 88);
			errLabelLData.top =  new FormAttachment(0, 1000, 38);
			errLabelLData.width = 162;
			errLabelLData.height = 14;
			errLabel.setLayoutData(errLabelLData);
			errLabel.setText("folder name has already exist!");
			errLabel.setForeground(SWTResourceManager.getColor(255, 0, 0));
			errLabel.setVisible(false);
		}
		//add the composite to the wizard
		addPage(inst, "Add new shared folder");
		//second composite definition
		Composite inst2 = new Composite(shell, SWT.NULL);
		FormLayout thisLayout2 = new FormLayout();
		inst2.setLayout(thisLayout2);
		inst2.setSize(406, 200);
		inst2.layout();
		{
			label = new Label(inst2, SWT.NONE );
			FormData labelLData = new FormData();
			labelLData.left =  new FormAttachment(0, 1000, 62);
			labelLData.top =  new FormAttachment(0, 1000, 102);
			labelLData.width = 147;
			labelLData.height = 96;
			label.setLayoutData(labelLData);
			label.setImage(SWTResourceManager.getImage("icons\\email.png"));//new Image(display, "icons\\email.png"));
		}
		
		
		{
			FormData emailListLData = new FormData();
			emailListLData.left =  new FormAttachment(0, 1000, 224);
			emailListLData.top =  new FormAttachment(0, 1000, 66);
			emailListLData.width = 144;
			emailListLData.height = 124;
			emailList = new List(inst2, SWT.NONE | SWT.BORDER);
			emailList.setLayoutData(emailListLData);
			
		}
		{
			addButton = new Button(inst2, SWT.PUSH);
			FormData addButtonLData = new FormData();
			addButtonLData.left =  new FormAttachment(0, 1000, 194);
			addButtonLData.top =  new FormAttachment(0, 1000, 70);
			addButtonLData.width = 23;
			addButtonLData.height = 20;
			addButton.setLayoutData(addButtonLData);
			addButton.setImage(SWTResourceManager.getImage("icons\\Button-Add-icon.png"));//new Image(display, "icons\\Button-Add-icon.png"));
			addButton.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					addButtonWidgetSelected(evt);
				}
			});
		}
		{
			FormData emailTextLData = new FormData();
			emailTextLData.left =  new FormAttachment(0, 1000, 21);
			emailTextLData.top =  new FormAttachment(0, 1000, 66);
			emailTextLData.width = 155;
			emailTextLData.height = 24;
			emailText = new Text(inst2, SWT.NONE | SWT.BORDER);
			emailText.setLayoutData(emailTextLData);
			emailText.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent evt) {
					emailTextKeyReleased(evt);
				}
			});
		}
		{
			emailLabel = new Label(inst2, SWT.NONE | SWT.WRAP | SWT.CENTER);
			FormData emailLabelLData = new FormData();
			emailLabelLData.left =  new FormAttachment(0, 1000, 12);
			emailLabelLData.top =  new FormAttachment(0, 1000, 26);
			emailLabelLData.width = 191;
			emailLabelLData.height = 36;
			emailLabel.setLayoutData(emailLabelLData);
			emailLabel.setText("Please insert an e-mail addres of a friend you want to share with:");
		}
		//add the composite to the wizard
		addPage(inst2, "Select friends to share");
	}
	/*
	 * init' wizard components
	 */
	private void init() {
		index = 0; // index for current page
		PAGES = new Vector<Control>();
		TITLES = new Vector<String>();
	}
	/*
	 * create the wizard  
	 */
	private void createGUI() {
		shell.setLayout(new GridLayout(3, false));
		
		wizardPanel = new Composite(shell, SWT.NONE);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 3;
		wizardPanel.setLayoutData(gd);
		
		wizardLayout = new StackLayout();
		wizardPanel.setLayout(wizardLayout);
		
		prev = new Button(shell, SWT.PUSH);
		next = new Button(shell, SWT.PUSH);
		cancelButton = new Button(shell, SWT.PUSH);
		cancelButton.setText("Cancel");
		cancelButton.setLayoutData(new GridData());
		cancelButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
			cancel();
			}
			});
		
		
		next.setText("Next >");
		next.setLayoutData(new GridData());
		next.addListener(SWT.Selection, new Listener() {
		public void handleEvent(Event e) {
			next.setEnabled(false);
			next();
		}
		});
		next.setEnabled(false);
		
		prev.setText("< Previous");
		prev.setLayoutData(new GridData());
		prev.addListener(SWT.Selection, new Listener() {
		public void handleEvent(Event e) {
		previous();
		}
		});
		prev.setEnabled(false);
	}
	/*
	 * "cancel" button selected
	 */
	private void cancel() {
		shell.setVisible(false);
		
	}
	/*
	 * "next" button selected
	 */
	private void next() {
		//the first page in wizard
		if(index==0){
			for (int i = 0; i < checkSet.size(); i++) {
				if(checkSet.get(i).getChecked()==true){
					System.out.println("checked: "+checkSet.get(i).getText());
					selectedFolders.add(checkSet.get(i).getText());
					
				}
			}
			
		}
		//last page
		if(index == PAGES.size()-1 && "finish".equalsIgnoreCase(next.getText())) {
			okButtonWidgetSelected();
			if(CommandsExcuter.getCommand().getAddFolder().getRetVal()){
				System.out.println("finished!");
				shell.setVisible(false);
				return;
			}else{
				emailList.removeAll();
				MessageBox messageBox = new MessageBox(shell, SWT.ICON_ERROR);
				messageBox.setMessage("folder name is already in use or\nemail address is invalid\nplease try again...");
				messageBox.open();
			}
		}
		//moving between pages
		if(index < PAGES.size()-1) {
			index += 1;
			wizardLayout.topControl = PAGES.get(index);
			shell.setText("Wizard - " + TITLES.get(index));
			wizardPanel.layout();
		}
		//not first page
		if(index-1 >= 0) {
			prev.setEnabled(true);
		}
		if(index+1 >= PAGES.size()) {
			
			next.setText("Finish");
		}
	}
	/*
	 * "previous" button selected
	 */
	private void previous() {
		//not first page
		if(index > 0) {
			index -= 1;
			wizardLayout.topControl = PAGES.get(index);
			next.setEnabled(true);
			shell.setText("Wizard - " + TITLES.get(index));
			wizardPanel.layout();
		}
		//not last page
		if(index+1 < PAGES.size()) {
			next.setText("Next >");
		}
		//first page
		if(index-1 < 0) {
			prev.setEnabled(false);
		}
	}
	
	/*
	 * add new page to wizard
	 */
	private void addPage(Control c, String title) {
	
		c.setParent(wizardPanel);
		PAGES.addElement(c);
		TITLES.addElement(title);
		wizardLayout.topControl = PAGES.get(0);
		shell.setText("Wizard - " + TITLES.get(0));
	}
	/*
	 * set the shell size
	 */
	private void setSize(int width, int height) {
		shell.setSize(450, 300);
	}
	/*
	 * "finish" button selected
	 */
	private void okButtonWidgetSelected() {
		ArrayList<String> emails=new ArrayList<String>();
		int size=emailList.getItemCount();
		for(int i=0;i<size;i++)
			emails.add(emailList.getItem(i));
		//excute "add shared folder" command
		AddFolderExcutor addFolderExcutor = CommandsExcuter.getCommand().getAddFolder();
		addFolderExcutor.setPath(folderNameText.getText());
		addFolderExcutor.setEmails(emails);
		if(existFolderOption.getSelection())
			addFolderExcutor.setFolders(selectedFolders);
		else
			addFolderExcutor.setFolders(null);
		addFolderExcutor.excute();
		
	}
	/*
	 * "add" button selected
	 */
	private void addButtonWidgetSelected(SelectionEvent evt) {
		emailList.add(emailText.getText());
		emailText.setText("");
		next.setEnabled(true);
	}
	/*
	 * "enter" key on email text box selected
	 */
	private void emailTextKeyReleased(KeyEvent evt) {
		if(evt.keyCode==13){
			emailList.add(emailText.getText());
			emailText.setText("");
		}
		
	}
}



